<!DOCTYPE html>
<html lang='en'>

<head>
    <meta charset='UTF-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <title>Document</title>
    <script src='../vue.global.js'></script>
</head>

<body>
    <div id="app">
        {{count}}
        <button @click="count++">add</button>

        <p>作者有书吗?</p>
        <span>{{ hasBooks() }}</span>
        <br>
        <span>{{ hasBooksComputed }}</span>
    </div>

    <script>
        Vue.createApp({
            data(){
                return {
                    count:0,
                    author: {
                        name: 'John Doe',
                        books: [
                            'Vue 2 - Advanced Guide',
                            'Vue 3 - Basic Guide',
                            'Vue 4 - The Mystery'
                        ]
                    }
                }
            },

            methods:{
                hasBooks(){
                    console.log("hasBooks");
                    return this.author.books.length > 0 ? "有书" : "无书"
                }
            },

            /* 
            计算属性:
            当组件更新时
            所依赖的一手数据项如果发生变化，则重新计算结果
            否则直接调用上一次的缓存
            */
            computed:{
                // 如果this.author.books发生变化 则调用函数重新计算结果
                hasBooksComputed(){
                    console.log("hasBooksComputed");
                    return this.author.books.length > 0 ? "有书" : "无书"
                }
            }

        }).mount("#app")

    </script>
</body>

</html>